package com.chunuo.blogs.shiro;

import com.chunuo.blogs.realm.UserRealm;
import com.chunuo.blogs.redis.MyRedisConfig;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.cache.RedisCacheManager;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        Map<String, String> map = new HashMap<>();

//        map.put("/favicon.ico", "anon");
//        map.put("/css/**", "anon");
//        map.put("/fonts/**", "anon");
//        map.put("/images/**", "anon");
//        map.put("/js/**", "anon");
//        map.put("/lib/**","anon");
//
//        map.put("/logout", "logout");
//        map.put("/login.html", "anon");
//        map.put("/user/login", "anon");

        map.put("/admin/login.html","anon");
        map.put("/logout", "logout");
        map.put("/admin/**","user");

        shiroFilter.setFilterChainDefinitionMap(map);

        shiroFilter.setSuccessUrl("/admin/indexList.html");
        shiroFilter.setLoginUrl("/admin/login.html");
        //        shiroFilter.setUnauthorizedUrl("/403.html");

        //设置注销后到登录页面，而不是到首页
        Map<String, Filter> filtersMap = new HashMap<>();
        LogoutFilter logoutFilter = new LogoutFilter();
        logoutFilter.setRedirectUrl("/admin/login.html");
        filtersMap.put("logout",logoutFilter);
        shiroFilter.setFilters(filtersMap);


        return shiroFilter;
    }


    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setSessionManager(sessionManager());
        securityManager.setAuthenticator(authenticator());
        securityManager.setRealm(userRealm());
        return securityManager;
    }

    @Bean
    public ModularRealmAuthenticator authenticator() {
        ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
        //如果有多个认证，需要每个认证都通过，默认只要通过一个认证即可
        authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        return authenticator;
    }

    /**
     * 3. 配置 Realm
     * 3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean
     * 将自己的验证方式加入容器
     *
     * @return
     */
    @Bean
    public UserRealm userRealm() {
        UserRealm userRealm = new UserRealm();
        HashedCredentialsMatcher hashedCM = new HashedCredentialsMatcher();
        hashedCM.setHashAlgorithmName("MD5");
        hashedCM.setHashIterations(1024);
        userRealm.setCredentialsMatcher(hashedCM);
        return userRealm;
    }

    @Bean
    public SessionManager sessionManager(){
        return new MySessionManager();
    }


    /**
     * 开启shiro aop注解支持.
     * 使用代理方式;所以需要开启代码支持;
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }


}
